package smtpServer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import dao.MailSendDAO;
import dto.MailSendDTO;

public class MailWrite {
	public static String str=null;
	private static PrintWriter pw=null;
	private static PrintWriter topw=null;
	private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss-SSS");
	public static String basePath="D:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/mailSystem/AllUsers";
    
	public static void writeMail(String FromName,String ToName,String Title,BufferedReader br){
		String date=sdf.format(new Date());
		MailSendDAO mdao=new MailSendDAO();
		String FromFileName=basePath+"/"+FromName+"/"+"send/"+date+".txt";
		String ToFileName=basePath+"/"+ToName+"/"+"receive/"+date+".txt";
		MailSendDTO msd=new MailSendDTO(FromName,ToName,Title,date,"1","1");
		File FromFile=new File(FromFileName);
		File ToFile=new File(ToFileName);
		try {
			if(!FromFile.exists()){
				FromFile.createNewFile();
			}
			pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(FromFile,true)));
			topw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(ToFile,true)));
			
			while(!(str=br.readLine()).equals(".")){
					topw.println(str);
					topw.flush();
					pw.println(str);
					pw.flush();
				}
			mdao.add(msd);
			topw.close();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
